package com.gdpu.service.impl;

import com.gdpu.api.CommonPage;
import com.gdpu.common.ApiException;
import com.gdpu.dao.PmsStockDao;
import com.gdpu.dto.OmsQueryParamDto;
import com.gdpu.enumClass.OrderStatus;
import com.gdpu.mapper.OmsOrderItemMapper;
import com.gdpu.mapper.OmsOrderMapper;
import com.gdpu.model.OmsOrder;
import com.gdpu.model.OmsOrderExample;
import com.gdpu.model.OmsOrderItem;
import com.gdpu.model.OmsOrderItemExample;
import com.gdpu.service.OmsOrderService;
import com.gdpu.vo.OmsOrderDetailVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author:weiming
 * @Date:2021/5/27
 */
@Service
public class OmsOrderServiceImpl implements OmsOrderService {

    Logger logger = LoggerFactory.getLogger(OmsOrderServiceImpl.class);
    @Autowired
    private OmsOrderMapper omsOrderMapper;
    @Autowired
    private OmsOrderItemMapper omsOrderItemMapper;
    @Autowired
    private PmsStockDao pmsStockDao;

    @Override
    public CommonPage list(Integer pageNum, Integer pageSize, OmsQueryParamDto omsQueryParamDto) {
        Page page = PageHelper.startPage(pageNum,pageSize);
        OmsOrderExample omsOrderExample = new OmsOrderExample();
        omsOrderExample.setOrderByClause("create_time desc");
        OmsOrderExample.Criteria criteria = omsOrderExample.createCriteria();
        if (omsQueryParamDto.getUsername()!=null){
            criteria.andUserNameEqualTo(omsQueryParamDto.getUsername());
        }
        if (omsQueryParamDto.getPhone()!=null){
            criteria.andUserPhoneEqualTo(omsQueryParamDto.getPhone());
        }
        if (omsQueryParamDto.getOrderSn()!=null){
            criteria.andOrderSnEqualTo(omsQueryParamDto.getOrderSn());
        }
        if (omsQueryParamDto.getStatus()!=null){
            criteria.andOrderStatusEqualTo(omsQueryParamDto.getStatus());
        }
        List<OmsOrder> omsOrders = omsOrderMapper.selectByExample(omsOrderExample);
        return CommonPage.restPage(omsOrders, page);
    }

    @Override
    public OmsOrderDetailVo get(Integer orderId) {
       OmsOrderDetailVo omsOrderDetailVo = new OmsOrderDetailVo();
        OmsOrder omsOrder = omsOrderMapper.selectByPrimaryKey(orderId);
        BeanUtils.copyProperties(omsOrder, omsOrderDetailVo);
        OmsOrderItemExample omsOrderItemExample = new OmsOrderItemExample();
        omsOrderItemExample.createCriteria().andOrderIdEqualTo(orderId);
        List<OmsOrderItem> orderItemList = omsOrderItemMapper.selectByExample(omsOrderItemExample);
        omsOrderDetailVo.setOmsOrderItems(orderItemList);
        return omsOrderDetailVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delivery(Integer orderId) {
        OmsOrder record = new OmsOrder();
        // 设置订单状态
        record.setId(orderId);
        record.setOrderStatus(OrderStatus.DELIVERING.getStatusCode());
        int count = 0;
        count = omsOrderMapper.updateByPrimaryKeySelective(record);
        // 库存表

        if (handleStock(orderId) == false) {
            logger.info("库存不足，发货失败");
            throw new ApiException("库存不足，发货失败");
        }
        return count;
    }

    private Boolean handleStock(Integer orderId) {
        // 获取商品
        boolean flag = true;
        OmsOrderItemExample example = new OmsOrderItemExample();
        example.createCriteria().andOrderIdEqualTo(orderId);
        List<OmsOrderItem> orderItemList = omsOrderItemMapper.selectByExample(example);
        // 商品库存表删除
        int result = pmsStockDao.updateStock(orderItemList);
        if (result != orderItemList.size()){
            flag = false;
        }
        return  flag;
    }


}
